package com.example.seakdreamandroid.recommendDay;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.seakdreamandroid.MusicStoryPlayerActivity;
import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.entity.AudioInfo;
import com.example.seakdreamandroid.entity.User;
import com.example.seakdreamandroid.whiteNoisePlayer.WhiteNoisePlayerActivity;
import com.example.seakdreamandroid.whiteNoisePlayer.WhiteNoisePlayerService;

import java.util.ArrayList;
import java.util.List;

public class PlayHistoryAdapter extends RecyclerView.Adapter {
    private List<HistoryRecord> data;
    private Activity activity;
    //记录当前正在播放的音频
    private int currentPlay = -1;
    private int purple_1;
    private int black;
    private int black_1;
    private static final int LAYOUT_ID = R.layout.history_item;
    private String userId;

    public int getCurrentPlay() {
        return currentPlay;
    }

    public void setCurrentPlay(int currentPlay) {
        this.currentPlay = currentPlay;
    }

    public PlayHistoryAdapter(List<HistoryRecord> data, Activity activity, String userId) {
        this.data = data;
        this.activity = activity;
        this.userId = userId;
        purple_1 = activity.getResources().getColor(R.color.purple_1);
        black = activity.getResources().getColor(R.color.black);
        black_1 = activity.getResources().getColor(R.color.black_1);
    }

    public void setData(List<HistoryRecord> mHistories) {
        data = mHistories;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(activity).inflate(R.layout.history_item,parent,false);
        return new MyHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        HistoryRecord history = data.get(position);
        MyHolder myHolder = (MyHolder)holder;
        if(currentPlay == position){
            myHolder.current.setVisibility(View.VISIBLE);
            myHolder.count.setVisibility(View.INVISIBLE);
            myHolder.title.setTextColor(purple_1);
            myHolder.type.setTextColor(purple_1);
        }else{
            myHolder.current.setVisibility(View.INVISIBLE);
            myHolder.count.setVisibility(View.VISIBLE);
            myHolder.count.setText(position + 1 + "");
            myHolder.title.setTextColor(black);
            myHolder.type.setTextColor(black_1);
        }
        if(history.isWhiteNoise()){
            myHolder.title.setText("白噪音");
            myHolder.type.setVisibility(View.INVISIBLE);
            myHolder.whiteNoises.setVisibility(View.VISIBLE);
            Log.i("PlayHistoryAdapter",history.getWhiteNoiseCount() +"");
            if(history.getWhiteNoiseCount() >= 1){
                ((ViewGroup)myHolder.whiteNoiseImg1.getParent()).setVisibility(View.VISIBLE);
                myHolder.whiteNoiseName1.setText(history.getWhiteNoiseName1());
                Glide.with(activity).load(history.getWhiteNoiseImg1()).into(myHolder.whiteNoiseImg1);
            }else{
                ((ViewGroup)myHolder.whiteNoiseImg1.getParent()).setVisibility(View.INVISIBLE);

            }
            if(history.getWhiteNoiseCount() >= 2){
                ((ViewGroup)myHolder.whiteNoiseImg2.getParent()).setVisibility(View.VISIBLE);
                myHolder.whiteNoiseName2.setText(history.getWhiteNoiseName2());
                Glide.with(activity).load(history.getWhiteNoiseImg2()).into(myHolder.whiteNoiseImg2);
            }else{
                ((ViewGroup)myHolder.whiteNoiseImg2.getParent()).setVisibility(View.INVISIBLE);
            }
            if(history.getWhiteNoiseCount() >= 3){
                ((ViewGroup)myHolder.whiteNoiseImg3.getParent()).setVisibility(View.VISIBLE);
                myHolder.whiteNoiseName3.setText(history.getWhiteNoiseName3());
                Glide.with(activity).load(history.getWhiteNoiseImg3()).into(myHolder.whiteNoiseImg3);
            }else{
                ((ViewGroup)myHolder.whiteNoiseImg3.getParent()).setVisibility(View.INVISIBLE);
            }
        }else{
            myHolder.title.setText(history.getAudioName());
            myHolder.type.setVisibility(View.VISIBLE);
            myHolder.whiteNoises.setVisibility(View.INVISIBLE);
            myHolder.type.setText(history.getAudioType());
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    private class MyHolder extends RecyclerView.ViewHolder {
        private TextView count;
        private ImageView current;
        private TextView title;
        private TextView type;
        private ImageView whiteNoiseImg1;
        private ImageView whiteNoiseImg2;
        private ImageView whiteNoiseImg3;
        private TextView whiteNoiseName1;
        private TextView whiteNoiseName2;
        private TextView whiteNoiseName3;
        private LinearLayout whiteNoises;
        private User user;
        MyHolder(View v){

            super(v);
            count = v.findViewById(R.id.count);
            current = v.findViewById(R.id.current);
            title = v.findViewById(R.id.title);
            type = v.findViewById(R.id.type);
            whiteNoiseImg1 = v.findViewById(R.id.white_noise_img1);
            whiteNoiseImg2 = v.findViewById(R.id.white_noise_img2);
            whiteNoiseImg3 = v.findViewById(R.id.white_noise_img3);
            whiteNoiseName1 = v.findViewById(R.id.white_noise_tex1);
            whiteNoiseName2 = v.findViewById(R.id.white_noise_tex2);
            whiteNoiseName3 = v.findViewById(R.id.white_noise_tex3);
            whiteNoises = v.findViewById(R.id.white_noises);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RecommendDayActivity activity = (RecommendDayActivity) PlayHistoryAdapter.this.activity;
                    int lastPlay = currentPlay;
                    currentPlay = getAdapterPosition();
                    notifyItemChanged(lastPlay);
                    notifyItemChanged(currentPlay);
                    /*
                    杨锐涵添加
                     */

                    user = (User) v.getContext().getApplicationContext();
                    int position = (int) getAdapterPosition();
                    HistoryRecord history = data.get(position);
                    if (history.isWhiteNoise()){
                        List<AudioInfo> audioInfos  = new ArrayList<>();
                        if (history.getWhiteNoiseCount()  >= 1){
                            AudioInfo audioInfo = new AudioInfo();
                            audioInfo.setAudioId(history.getWhiteId1());
                            audioInfo.setAudioName(history.getWhiteNoiseName1());
                            audioInfo.setAudioPic(history.getWhiteNoiseImg1());
                            audioInfo.setAudioUrl(history.getWhiteNoiseUrl1());
                            audioInfos.add(audioInfo);
                        }
                        if (history.getWhiteNoiseCount()  >= 2){
                            AudioInfo audioInfo = new AudioInfo();
                            audioInfo.setAudioId(history.getWhiteId2());
                            audioInfo.setAudioName(history.getWhiteNoiseName2());
                            audioInfo.setAudioPic(history.getWhiteNoiseImg2());
                            audioInfo.setAudioUrl(history.getWhiteNoiseUrl2());
                            audioInfos.add(audioInfo);
                        }
                        if (history.getWhiteNoiseCount()  >= 3){
                            AudioInfo audioInfo = new AudioInfo();
                            audioInfo.setAudioId(history.getWhiteId3());
                            audioInfo.setAudioName(history.getWhiteNoiseName3());
                            audioInfo.setAudioPic(history.getWhiteNoiseImg3());
                            audioInfo.setAudioUrl(history.getWhiteNoiseUrl3());
                            audioInfos.add(audioInfo);
                        }
                        //播放
                        WhiteNoisePlayerService.getController().play(audioInfos,"playHistory",null,true);
                        //跳转
                        Intent intent = new Intent(activity, WhiteNoisePlayerActivity.class);
                        activity.startActivityForResult(intent, 1);
                    }else{
                        Intent intent = new Intent(activity,MusicStoryPlayerActivity.class);
                        intent.putExtra("audioUrl",history.getAudioUrl());
                        intent.putExtra("audioId",history.getAudioId());
                        intent.putExtra("audioName",history.getAudioName());
                        intent.putExtra("audioAuthor",history.getAudioAuthor());
                        activity.startActivity(intent);
                    }


                }
            });
        }
    }
}
